package org.projectwithoutname.application;


import org.eclipse.core.runtime.IExtension;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.registry.ActionSetRegistry;
import org.eclipse.ui.internal.registry.IActionSetDescriptor;

/**
 * An action bar advisor is responsible for creating, adding, and disposing of the
 * actions added to a workbench window. Each window will be populated with
 * new actions.
 */
@SuppressWarnings("restriction")
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

    // Actions - important to allocate these only in makeActions, and then use them
    // in the fill methods.  This ensures that the actions aren't recreated
    // when fillActionBars is called with FILL_PROXY.
    public ApplicationActionBarAdvisor(IActionBarConfigurer configurer){
       super(configurer);
              
       ActionSetRegistry reg = WorkbenchPlugin.getDefault().getActionSetRegistry();
       IActionSetDescriptor[] actionSets = reg.getActionSets();
       for(int i=0; i<actionSets.length; i++)
       {
	       removeStandardAction(reg, actionSets[i].getId());
       }	      
    }
    
    private void removeStandardAction(ActionSetRegistry reg, String actionSetId) 
    {
    	IActionSetDescriptor[] actionSets = reg.getActionSets();
    	for (int i = 0; i <actionSets.length; i++)
        {
     	   if (actionSets[i].getId().equals(actionSetId))
     	   {
     		   IExtension ext = actionSets[i].getConfigurationElement().getDeclaringExtension();
     		   reg.removeExtension(ext, new Object[] { actionSets[i] });
     		   break;
     	   }
        }
    }
    
    protected void makeActions(final IWorkbenchWindow window) {
     
    	IWorkbenchAction exitAction;
        IWorkbenchAction aboutAction;
        IWorkbenchAction newWindowAction;
        
    	IWorkbenchAction save = ActionFactory.SAVE.create(window);
        register(save);

        IWorkbenchAction saveAll = ActionFactory.SAVE_ALL.create(window);
        register(saveAll);    	
        
        IWorkbenchAction print = ActionFactory.PRINT.create(window);
    	register(print);

        exitAction = ActionFactory.QUIT.create(window);
        register(exitAction);
        
        aboutAction = ActionFactory.ABOUT.create(window);
        register(aboutAction);
        
        newWindowAction = ActionFactory.OPEN_NEW_WINDOW.create(window);
        register(newWindowAction);
    }
    
    protected void fillMenuBar(IMenuManager menuBar) {
    }
    
    protected void fillCoolBar(ICoolBarManager coolBar) {
    }
}